package com.company;

import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @date : 2023-03-23 下午2:13
 * 使用 FileInputStream 读取大文件
 * JVM参数
 * -Xms2g -Xmx2g -XX:+PrintGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps
 * 9亿行文件读取不出来
 * 1亿行数据 68秒
 */


@Slf4j
public class P047_ReadBigFileWithFileInputStream {

    public static void main(String[] args) {
        try {
            Thread.sleep(30000);
        } catch (Exception e) {
            e.printStackTrace();
        }

        long start = System.nanoTime();
        File file = new File("file/new.txt");
        List<String> list = new ArrayList<>();
        try (FileInputStream inputStream = new FileInputStream(file);
             Scanner sc = new Scanner(inputStream)) {
            while (sc.hasNextLine()) {
                String line = sc.nextLine();
                if (line.contains("99999999")) {
                    list.add(line);
                }
            }
        } catch (Exception e) {
            log.error("文件读取异常", e);
        }
        log.info("list:{}", list);
        long time = System.nanoTime() - start;
        log.info("读取文件数据耗时：{} s", time / 1000.0 / 1000 / 1000);
    }
}
